package com.blog.servlet;

import java.io.IOException;
import java.util.Vector;

import javax.print.attribute.standard.RequestingUserName;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.blog.dao.CategoryDao;
import com.blog.dto.Category;

public class CategoryServlet extends HttpServlet {

	protected void addCategory(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		int category_id = Integer .parseInt(req.getParameter("category_id"));
		String category = req.getParameter("category");
		int category_order = Integer .parseInt(req.getParameter("category_order"));
		Category cate=new Category();
		cate.setCategory(category);
//		cate.setCategory_id(category_id);
		cate.setCategory_order(category_order);
		CategoryDao categoryDao = new CategoryDao();
		if(categoryDao.addCategory(cate)) {
			findAllCategory(req, resp);
			req.getRequestDispatcher("/admin/OKAndClose.html").forward(req, resp);
		}else {
			resp.getWriter().append("分类名称已存在!");
		}
		return ;
	}
	
	protected void delCategory(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int category_id = Integer .parseInt(req.getParameter("category_id"));
		CategoryDao categoryDao = new CategoryDao();
		categoryDao.delCategoryById(category_id);
		findAllCategory(req, resp);
		return;
	}
	
	protected void editCategory(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int category_id = Integer .parseInt(req.getParameter("category_id"));
		String category = req.getParameter("category");
		int category_order = Integer .parseInt(req.getParameter("category_order"));
		Category cate = new Category();
		cate.setCategory(category);
		cate.setCategory_id(category_id);
		cate.setCategory_order(category_order);
		CategoryDao categoryDao = new CategoryDao();
		if(categoryDao.editCategory(cate)) {
			findAllCategory(req, resp);
			req.getRequestDispatcher("/admin/OKAndClose.html").forward(req, resp);
		}else {
			resp.getWriter().append("分类名称已存在!");
		}
		return ;
	}
	
	
	protected void findAllCategory(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		CategoryDao categoryDao = new CategoryDao();
		Vector<Category> category=new Vector<Category>();
		category = categoryDao.findAllCategory();
		HttpSession session  =  req.getSession();
		session.setAttribute("allCategory", category);
		return ;
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String act = null;
		act=req.getParameter("act");
		
		if(act==null || act.equals("")) act = "index";
		System.out.println("CategoryServlet act = "+act);
		
		switch (act) {
		case "add":
			addCategory(req, resp);
			break;
		case "del":
			delCategory(req, resp);
			break;
		case "edit":
			editCategory(req, resp);
			break;
		case "findAllCategory":
			findAllCategory(req, resp);
			resp.sendRedirect(req.getContextPath()+"/admin/article-category-list.jsp");
			break;
		case "findUserByName":
			
			break;

		default:
			findAllCategory(req, resp);
//			页面重定向
			resp.sendRedirect(req.getContextPath()+"/admin/article-category-list.jsp");
			break;
		}
	}

}
